package com.qf.controller;

import com.qf.commons.TableData;
import com.qf.entity.Goods;
import com.qf.exception.BizException;
import com.qf.exception.ControllerException;
import com.qf.service.GoodsService;
import com.qf.utils.StringUtils;
import org.jctools.maps.NonBlockingHashMap;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@CrossOrigin("*")
@RestController
@RequestMapping(("/goods"))
public class GoodsController {
    @Value("${fileDownloadLocation}")
    private  String fileDownloadLocation;

    private GoodsService goodsService;


    public GoodsController(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    @GetMapping()
    public Object getPageAll(@RequestParam(defaultValue = "0") Integer offset,@RequestParam(defaultValue = "10") Integer limit){
        TableData<Goods> tableData =null;
      //  NonBlockingHashMap<String,Object> map = new NonBlockingHashMap<>();
        try {
           tableData = goodsService.queryAll(offset, limit);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ControllerException("控制层商品查询错误："+e.getMessage());
        }

//        if (tableData!=null){
//            map.put("code",1);
//            map.put("data",tableData);
//        }else {
//            map.put("code",0);
//            map.put("msg","查询出错，稍后再试。。。");
//        }
        return tableData;
    }


    @GetMapping("/id/{goodsId}")
    public Object queryById(@PathVariable("goodsId") Integer id){
        Goods goods = new Goods();
        goods.setGoodsId(id);
        List<Goods> goodsList=null;
        try {
            goodsList= goodsService.dynamicQuery(goods);
        }catch (BizException e){
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ControllerException("控制层根据id查询商品错误：" + e.getMessage());
        }

        return goodsList.get(0);
    }

    @PostMapping("/info")
    public Object DynamicChange(Goods goods) {
        Integer integer = 0;
        goods.setUpdateTime(new Date());
        NonBlockingHashMap<String, Object> map = new NonBlockingHashMap<>();
        try {
            integer = goodsService.dynamicChange(goods);
        } catch (BizException e){
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ControllerException("控制层商品修改错误：" + e.getMessage());
        }

        map.put("code",integer);
        if (integer>0){
            map.put("msg","修改成功");
        }else{
            map.put("msg","修改出错，稍后再试。。。");
        }

        return map;
    }

    @PostMapping("/infos")
    public Object addGoods(Goods goods){
        Integer integer = 0;
        goods.setCreateTime(new Date());
        goods.setUpdateTime(new Date());
        NonBlockingHashMap<String, Object> map = new NonBlockingHashMap<>();
        try {
            integer = goodsService.addGoods(goods);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ControllerException("控制层添加商品错误：" + e.getMessage());
        }

        map.put("code",integer);
        if (integer>0){
            map.put("msg","添加成功");
        }else{
            map.put("msg","添加出错，稍后再试。。。");
        }

        return map;
    }



    @PostMapping("/headImg")
    public Object uploadHeadImg(@RequestParam("displayImage") MultipartFile headImg,String itemName){
        // 获取文件的原始名字
        String originFileName = headImg.getOriginalFilename();

        // 获取文件的后缀： .png .jpg XXXX
        String fileSuffix = originFileName.substring(originFileName.lastIndexOf("."));

        // 新的文件名
        String newFileName =
                itemName+"/"+ System.nanoTime() + StringUtils.generateRandomString(10) + fileSuffix;
        String filePath = fileDownloadLocation + newFileName;
        try {
            headImg.transferTo(new File(filePath));  // D:/nginx-server/images/8877776656afeji786766.png

        } catch (IOException ioException) {
            throw new ControllerException(ioException.getMessage());
        }
        NonBlockingHashMap<String,Object> map =new NonBlockingHashMap<>();
        map.put("urlPath",newFileName);
        return map;
    }

    @GetMapping("/goodsCart/{uId}")
    public Object queryGoodsCart(@PathVariable("uId") Integer uId){
        TableData<Goods> tableData = null;
        try {
            tableData= goodsService.queryGoodsCart(uId);
        }catch (BizException e){
          throw e;
        } catch(Exception e) {
            e.printStackTrace();
            throw new ControllerException("控制层查询商品购物车错误：" + e.getMessage());
        }
        return tableData;
    }
}
